/*******************************************************************************
 * Copyright (c) 2013 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.pizzaorder;

import nz.co.senanque.pizzaorder.instances.Order;
import nz.co.senanque.vaadinsupport.application.MaduraSessionManager;
import nz.co.senanque.vaadinsupport.formatting.FormattingTable2;

import org.springframework.context.support.MessageSourceAccessor;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ShoppingCartLayout extends CustomComponent {
	
    @AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_2;
	@AutoGenerated
	private TextField totalField;
	@AutoGenerated
	private FormattingTable2 table_1;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	public static final String[] NATURAL_COL_ORDER = new String[] {"description","-amount" };
	public static final String[] ENGLISH_COL_ORDER = new String[] {"Description","Amount" };
	private final MessageSourceAccessor m_messageSourceAccessor;
    private final MaduraSessionManager m_maduraSessionManager;

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public ShoppingCartLayout(MaduraSessionManager maduraSessionManager) {
		m_maduraSessionManager = maduraSessionManager;
		m_messageSourceAccessor = m_maduraSessionManager.getMessageSourceAccessor();
		buildMainLayout();
		setCompositionRoot(mainLayout);
		getMaduraSessionManager().register(totalField);
	}

	public void setOrderItems(BeanItemContainer indexed, Order order) {
		MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		table_1.setContainerDataSource(indexed,NATURAL_COL_ORDER,ENGLISH_COL_ORDER,m_messageSourceAccessor);
		maduraSessionManager.bind(totalField, maduraSessionManager.getMaduraPropertyWrapper(order, "amount"));
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// table_1
		table_1 = new FormattingTable2();
		table_1.setImmediate(false);
		table_1.setWidth("500px");
		table_1.setHeight("240px");
		mainLayout.addComponent(table_1);
		
		// horizontalLayout_2
		horizontalLayout_2 = buildHorizontalLayout_2();
		mainLayout.addComponent(horizontalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_2() {
		// common part: create layout
		horizontalLayout_2 = new HorizontalLayout();
		horizontalLayout_2.setImmediate(false);
		horizontalLayout_2.setWidth("100.0%");
		horizontalLayout_2.setHeight("-1px");
		horizontalLayout_2.setMargin(false);
		
		// totalField
		totalField = new TextField();
		totalField.setStyleName("v-textfield-align-right");
		totalField.setImmediate(false);
		totalField.setWidth("-1px");
		totalField.setHeight("-1px");
		totalField.setSecret(false);
		horizontalLayout_2.addComponent(totalField);
		horizontalLayout_2.setComponentAlignment(totalField, new Alignment(34));
		
		return horizontalLayout_2;
	}

	public MaduraSessionManager getMaduraSessionManager() {
		return m_maduraSessionManager;
	}


}
